﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperCalc
{
    /// <summary>
    /// 计算类
    /// </summary>
    public class Calculate
    {
        #region 加法运算
        /// <summary>
        /// 加法运算
        /// </summary>
        /// <param name="addend">加数</param>
        /// <param name="augend">被加数</param>
        /// <param name="msg">消息</param>
        /// <returns>运算结果</returns>
        public decimal Addition(string addend, string augend, out string msg)
        {
            msg = "";
            decimal a = 0;//加数
            decimal b = 0;//被加数

            if (!decimal.TryParse(addend, out a))  //转换加数
            {
                msg = "加数字符串转换失败。";
                return 0;
            }
            if (!decimal.TryParse(augend, out b))  //转换被加数
            {
                msg = "被加数字符串转换失败。";
                return 0;
            }
            return a + b;
        }
        #endregion

        #region 减法运算
        /// <summary>
        /// 减法运算
        /// </summary>
        /// <param name="minuend">被减数</param>
        /// <param name="subtrahend">减数</param>
        /// <param name="msg">消息</param>
        /// <returns>运算结果</returns>
        public decimal Subtraction(string minuend, string subtrahend, out string msg)
        {
            msg = "";
            decimal a = 0;//被减数
            decimal b = 0;//减数
            if (!decimal.TryParse(minuend, out a))  //转换被减数
            {
                msg = "被减数转换失败。";
                return 0;
            }
            if (!decimal.TryParse(subtrahend, out b))  //转换减数
            {
                msg = "减数转换失败。";
                return 0;
            }
            return a - b;
        }
        #endregion

        #region 乘法运算
        /// <summary>
        /// 乘法运算
        /// </summary>
        /// <param name="multiplicand">被乘数</param>
        /// <param name="multiplier">乘数</param>
        /// <param name="msg">消息</param>
        /// <returns>运算结果</returns>
        public decimal Multiplication(string multiplicand, string multiplier, out string msg)
        {
            decimal a = 0;
            decimal b = 0;
            msg = "";
            if (!decimal.TryParse(multiplicand, out a))  //转换被减数
            {
                msg = "被乘数转换失败。";
                return 0;
            }
            if (!decimal.TryParse(multiplier, out b))  //转换减数
            {
                msg = "乘数转换失败。";
                return 0;
            }
            return a * b;
        }
        #endregion

        #region 除法运算
        /// <summary>
        /// 除法运算
        /// </summary>
        /// <param name="dividend">被除数</param>
        /// <param name="divisor">除数</param>
        /// <param name="msg">消息</param>
        /// <returns>运算结果</returns>
        public decimal Division(string dividend, string divisor, out string msg)
        {
            decimal a = 0;
            decimal b = 0;
            msg = "";
            if (!decimal.TryParse(dividend, out a))  //转换被减数
            {
                msg = "被除数转换失败。";
                return 0;
            }
            if (!decimal.TryParse(divisor, out b))  //转换减数
            {
                msg = "除数转换失败。";
                return 0;
            }
            if (b == 0)
            {
                msg = "除数不能为0。";
                return 0;
            }
            return a / b;
        }
        #endregion
    }
}
